if (!OMSystem) var OMSystem = { };
if (!OMSystem.OrkutManager) OMSystem.OrkutManager = { };
if (!OMSystem.OrkutManager.XULOptions) OMSystem.OrkutManager.XULOptions = { };

OMSystem.OrkutManager.XULOptions =
{
	Colorpicker:
	{
		SetColorText:
			function ()
			{
				var color = document.getElementById("Colorpicker").color;
				document.getElementById("SelectedColor").value = color;
			},

		SetColor:
			function (value)
			{
				var color = document.getElementById("Colorpicker");
				color.color = value;
			}
	},

	DateTime:
	{
		PreviewTime:
			function ()
			{
				var v = document.getElementById("TimeFormat");
				var p = document.getElementById("TimeFormatPreview");
				
				function omTime(format)
				{
					format = unescape(format);
					function LeadZero(v) { return (v < 10 ? '0' + v : v); } var date = new Date();  var d = LeadZero(date.getDate()); var j = date.getDate(); var w = date.getDay(); var l; l = "Saturday"; var D = l.substring(0, 3);  var m = LeadZero(date.getMonth() + 1); var n = (date.getMonth() + 1); var F; F = "May"; var M = F.substring(0, 3);  function YearLeap(y) {  if ((y % 4) == 0)  {   if ((y % 100) == 0)    return ((y % 400) == 0) ? 1 : 0;   else    return 1;  }  else return 0; }  var Y = date.getFullYear() + ''; var y = Y.substring(2); var L = YearLeap(Y); var H = date.getHours(); var h = (H > 12) ? (H - 12) : (H); var G = H; var g = h; var A = (H > 12) ? 'PM' : 'AM'; var a = A.toLowerCase(); H = LeadZero(H); h = LeadZero(h);  var i = LeadZero(date.getMinutes());  var s = LeadZero(date.getSeconds()); var B = date.getMilliseconds();  return format.replace(/%d/g, d).replace(/%j/g, j).replace(/%w/, w).replace(/%l/g, l).replace(/%D/g, D).replace(/%m/g, m).replace(/%n/g, n).replace(/%F/g, F).replace(/%M/g, M).replace(/%Y/g, Y).replace(/%y/g, y).replace(/%L/g, L).replace(/%H/g, H).replace(/%h/g, h).replace(/%G/g, G).replace(/%g/g, g).replace(/%A/g, A).replace(/%a/g, a).replace(/%i/g, i).replace(/%s/g, s).replace(/%B/g, B);
				}
					p.value = omTime(v.value);
			}
	},

	Dialog:
	{
		Save:
			function ()
			{
				OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Save('MenuDD');
				OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Save('MenuHD');
			},

		SetApply: function (enabled) { document.documentElement.getButton("extra1").disabled = !enabled; },

		Apply:
			function ()
			{
				OMSystem.OrkutManager.PrefManager.pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.orkutmanager.");
				
				var prefs = document.getElementsByTagName('preference');
				for (var i = 0; i < prefs.length; ++i)
				{
					prefs[i].valueFromPreferences = prefs[i].value;
				}
				
				OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Save('MenuDD');
				OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Load('MenuDD');
				OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Save('MenuHD');
				OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Load('MenuHD');

				OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Save('EmoticonsList');
				OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Load('EmoticonsList');
								
				OMSystem.OrkutManager.XULOptions.Dialog.SetApply(false);
			},

		Import:
			function (e)
			{
				var File = OMSystem.OrkutManager.Util.File;
				var err;
				try
				{
					var fp = File.Choose("Import", "Open");
					var cfgs = File.Read(fp);
					if (!cfgs) throw "";
					OMSystem.OrkutManager.PrefManager.Import(cfgs);
				}
				catch (ex) { err = ex; }
				if (err) window.alert(err);

				e.handled = false;
			},

		Export:
			function (e)
			{
				var File = OMSystem.OrkutManager.Util.File;
				var err;
				try
				{
					var output = OMSystem.OrkutManager.PrefManager.Export();
					if (!output) throw "";
					File.Write(File.Choose("Export", "Save"), output);
				}
				catch (ex) { err = ex; }
				if (err) window.alert(err);

				e.handled = false;
			},

		Defaults:
			function (e)
			{
				var err;
				try
				{
					OMSystem.OrkutManager.PrefManager.Reset();
					OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Load('MenuDD');
					OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Load('MenuHD');
				}
				catch (ex) { err = ex; }
				if (err) window.alert(err);
				
				e.handled = false;
			},

		OMMenu:
		{
			Save :
				function (Id)
				{
					try
					{
						OMSystem.OrkutManager.PrefManager.pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.orkutmanager.");
					}
					catch (ex) { }
					try
					{
						var node = "";
						var lis = document.getElementById(Id).getElementsByTagName("listitem");
						var vals = [];
						for each(var li in lis)
						{
							if (!li || !li.getAttribute) continue;
							var Name = (li.getAttribute("valueName").toString());
							var Link = (li.getAttribute("value").toString());
							vals.push(escape(Name) + "|" + escape(Link));
						}
						OMSystem.OrkutManager.PrefManager.Set(Id, uneval(vals));
					} catch (ex) { }
				},
			Load :
				function (Id)
				{
					var listbox = document.getElementById(Id);
					var prefMenu = JSON.parse(OMSystem.OrkutManager.PrefManager.Get(Id, ""));

					var item = "";
					while ((item = listbox.getElementsByTagName("listitem")))
					{
						if (item.length < 1) break;
						try
						{
							listbox.removeChild(item[0]);
						} catch (ex) { }
					}
					
					if (prefMenu.length < 1) return;
					for each(var pm in prefMenu)
					{
						var li = document.createElement("listitem");
						li.setAttribute("label", unescape(pm.split("|")[0]) + " (" + unescape(pm.split("|")[1]) + ")");
						li.setAttribute("valueName", unescape(pm.split("|")[0]));
						li.setAttribute("value", unescape(pm.split("|")[1]));
						listbox.appendChild(li);
					}
				},

			LoadNode :
				function (SelectedIndex, Id)
				{
					if (SelectedIndex < 0) return;
					var li = document.getElementById(Id).selectedItem;
					var Name = li.getAttribute("valueName");
					var Link = li.getAttribute("value");
					document.getElementById(Id + "Name").value = Name;
					document.getElementById(Id + "Link").value = Link;
				},
				
			Add :
				function (Id)
				{
					var Name = document.getElementById(Id + "Name").value;
					var Link = document.getElementById(Id + "Link").value;
					if (Name.length < 1) return;
					if (Link.length < 1) return;
					var li = document.createElement("listitem");
					li.setAttribute("label", Name + " (" + Link + ")");
					li.setAttribute("valueName", Name);
					li.setAttribute("value", Link);
					
					var lb = document.getElementById(Id);
					lb.appendChild(li);
					var lastIndex = (lb.getElementsByTagName("listitem").length - 1);
					lb.selectedIndex = lastIndex;
					lb.ensureIndexIsVisible(lastIndex);
				},
			
			Del :
				function (Id)
				{
					var SelectedIndex = document.getElementById(Id).selectedIndex;
					if (SelectedIndex < 0) return;
					var li = document.getElementById(Id).selectedItem;
					
					document.getElementById(Id).selectedIndex = (SelectedIndex - 1);
					
					return document.getElementById(Id).removeChild(li);
				},
			
			Edit :
				function (Id)
				{
					var SelectedIndex = document.getElementById(Id).selectedIndex;
					if (SelectedIndex < 0) return;
					var Name = document.getElementById(Id + "Name").value;
					var Link = document.getElementById(Id + "Link").value;
					if (Name.length < 1) return;
					if (Link.length < 1) return;
					var li = OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Del(Id);
					li.setAttribute("label", Name + " (" + Link + ")");
					li.setAttribute("valueName", Name);
					li.setAttribute("value", Link);
					
					document.getElementById(Id).insertBefore(li, document.getElementById(Id).getElementsByTagName("listitem")[SelectedIndex]);
					document.getElementById(Id).selectedIndex = SelectedIndex;
				},
			
			SetSeparator :
				function (Id)
				{
					var Name = document.getElementById(Id + "Name");
					var Link = document.getElementById(Id + "Link");
					Name.value = "-";
					Link.value = "javascript:;";
				},
			
			Move :
				function (Step, Id)
				{
					var elementCount = document.getElementById(Id).getElementsByTagName("listitem").length;
					var SelectedIndex = document.getElementById(Id).selectedIndex;
					if (SelectedIndex < 0) return;
					var li = document.getElementById(Id).selectedItem;
					
					if (SelectedIndex >= 0 && SelectedIndex <= elementCount)
					{
						if (SelectedIndex == 0 && Step == -1) return;
						if (SelectedIndex == elementCount && Step == 1) return;
						try
						{
							document.getElementById(Id).insertBefore(li, (Step == -1 ? li.previousSibling : li.nextSibling.nextSibling));
							document.getElementById(Id).selectedIndex = SelectedIndex + Step;
						}
						catch (ex) { }
					}
				},
				
			Keydown :
				function (e, Id)
				{
					if (e.keyCode == 46)
					{
						OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Del(Id);
						OMSystem.OrkutManager.XULOptions.Dialog.SetApply(true);
					}
					if (e.ctrlKey && !e.altKey && !e.shiftKey)
					{
						if (e.keyCode == 38) { OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Move(-1, Id); OMSystem.OrkutManager.XULOptions.Dialog.SetApply(true); }
						if (e.keyCode == 40) { OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Move(1, Id); OMSystem.OrkutManager.XULOptions.Dialog.SetApply(true); }
						e.handled = true;
					}
				}
		},

		OnModDeleteOnCommand:
			function ()
			{
				var el = document.getElementById("OnModDelete");
				document.getElementById("OnModDeleteConfirm").disabled = !el.checked;
			},
		OnDeleteModPostOnCommand:
			function ()
			{
				var el = document.getElementById("OnDeleteModPost");
				document.getElementById("OnModModConfirm").disabled = !el.checked;
			},

		InitializeComponents:
			function ()
			{
                try
                {
                    var gExtensionManager = Components.classes["@mozilla.org/extensions/manager;1"]
                        .getService(Components.interfaces.nsIExtensionManager);
                    var current = gExtensionManager.getItemForID("om.brunolm@gmail.com").version;
                    document.title = "Orkut Manager v" + current;
                }
                catch (ex) { }
				try
				{
					OMSystem.OrkutManager.XULOptions.Dialog.SetApply(false);
					OMSystem.OrkutManager.XULOptions.Dialog.OnModDeleteOnCommand();
					OMSystem.OrkutManager.XULOptions.Dialog.OnDeleteModPostOnCommand();
					OMSystem.OrkutManager.XULOptions.DateTime.PreviewTime();
				}
				catch (ex) { }
				
				try
				{
					OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Load("MenuDD");
					OMSystem.OrkutManager.XULOptions.Dialog.OMMenu.Load("MenuHD");
				}
				catch (ex) { }
				
				try
				{
					document.documentElement.getButton("extra2").style.display = "none";
					function MoveElementDown(b)
					{
						var ref = document.documentElement.getButton("extra2");
						b.className = ref.className;
						ref.parentNode.insertBefore(b, ref);
						b.hidden = false;
					}
					MoveElementDown(document.getElementById("command"));
				}
				catch (ex) { }
			}
	}
};
